package Servlets.lhl;/**
 * 项目名称：PurSearchServlet.java
 * 文件名称：PurSearchServlet.java
 * 类的描述：无
 * 创建时间：2025-07-26 16:00:05
 * 创建人：大帅比
 * <p>
 * 版权声明：本文件归 大帅比 所有，未经授权不得进行商业使用。
 */
 /*
                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O\  =  /O
               ____/`---'\____
             .'  \\|     |//  `.
            /  \\|||  :  |||//  \
           /  _||||| -:- |||||-  \
           |   | \\\  -  /// |   |
           | \_|  ''\---/''  |   |
           \  .-\__  `-`  ___/-. /
         ___`. .'  /--.--\  `. . __
      ."" '<  `.___\_<|>_/___.'  >'"".
     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
     \  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
            佛祖保佑       永无BUG
*/

import java.io.*;
import java.util.ArrayList;
import java.util.List;

import Bean.purchase_order;
import Util.GokJDBCUtil2;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@WebServlet(name = "PurSearchServlet", value = "/ps")
public class PurSearchServlet extends HttpServlet {

    public void init() {
    }

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
        String name = req.getParameter("name");
        String orderStatus = req.getParameter("orderStatus");
        String startDate = req.getParameter("startDate");
        String endDate = req.getParameter("endDate");
        StringBuilder sql = new StringBuilder(
                "select id,code,supplierid,(select supplier.name from supplier where purchase_order.supplierid=supplier.id  ) as suname " +
                        ",ordertime,totalamount,orderstatus,remark from purchase_order where 1=1" );
        List<String> pamar =new ArrayList<>();
        if(name != null && !name.isEmpty()){
            sql.append(" AND EXISTS (SELECT 1 FROM supplier WHERE supplier.id = purchase_order.supplierid AND name like ?) ");
            pamar.add("%"+name.trim()+"%");
        }
        if(orderStatus != null && !orderStatus.isEmpty()){
            sql.append(" AND orderstatus = ? ");
            pamar.add(orderStatus.trim());
        }
        if(startDate != null && !startDate.isEmpty()){
            sql.append(" AND ordertime >= ? ");
            pamar.add(startDate.trim()+" 00:00:00");
        }
        if(endDate != null && !endDate.isEmpty()){
            sql.append(" AND ordertime <= ? ");
            pamar.add(endDate.trim() +" 23:59:59");
        }
        sql.append(" AND isdeleted = 0");
        List<purchase_order> purchaseOrders = GokJDBCUtil2.queryAll(sql.toString(), purchase_order.class, pamar.toArray());
        req.setAttribute("list", purchaseOrders);
        //设置总页数
        int i = GokJDBCUtil2.queryForCount("select count(*) from purchase_order where isdeleted = 0");
        req.setAttribute("count", i%10==0?i/10:i/10+1);
        req.getRequestDispatcher("pages/purchase.jsp").forward(req,res);
    }
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        String method = req.getMethod();
        try {
            if (method.equals("GET")) {
                this.doGet(req, res);
            }else {
                PrintWriter writer = res.getWriter();
                writer.println("{\"code\":\"400\",\"msg\":\"此方法不允许\"}");
                writer.close();
            }
        } catch (Exception e) {
            log.error("服务器处理请求时发生错误", e);
            PrintWriter writer = res.getWriter();
            writer.println("{\"code\":\"500\",\"msg\":\"服务器处理请求时发生错误\"}");
            writer.close();
        }
    }

    public void destroy() {
    }
}